<template>
  <el-dialog
    :title="!dataForm.mbId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="myUserId">
      <el-input v-model="dataForm.myUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="书架数量" prop="myNum">
      <el-input v-model="dataForm.myNum" placeholder="书架数量"></el-input>
    </el-form-item>
    <el-form-item label="书名" prop="myBookName">
      <el-input v-model="dataForm.myBookName" placeholder="书名"></el-input>
    </el-form-item>
    <el-form-item label="书封面" prop="myBookCover">
      <el-input v-model="dataForm.myBookCover" placeholder="书封面"></el-input>
    </el-form-item>
    <el-form-item label="书内容" prop="myBookContentId">
      <el-input v-model="dataForm.myBookContentId" placeholder="书内容"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          mbId: 0,
          myUserId: '',
          myNum: '',
          myBookName: '',
          myBookCover: '',
          myBookContentId: ''
        },
        dataRule: {
          myUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          myNum: [
            { required: true, message: '书架数量不能为空', trigger: 'blur' }
          ],
          myBookName: [
            { required: true, message: '书名不能为空', trigger: 'blur' }
          ],
          myBookCover: [
            { required: true, message: '书封面不能为空', trigger: 'blur' }
          ],
          myBookContentId: [
            { required: true, message: '书内容不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.mbId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.mbId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbmybookshelf/info/${this.dataForm.mbId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.myUserId = data.tbMyBookshelf.myUserId
                this.dataForm.myNum = data.tbMyBookshelf.myNum
                this.dataForm.myBookName = data.tbMyBookshelf.myBookName
                this.dataForm.myBookCover = data.tbMyBookshelf.myBookCover
                this.dataForm.myBookContentId = data.tbMyBookshelf.myBookContentId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbmybookshelf/${!this.dataForm.mbId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'mbId': this.dataForm.mbId || undefined,
                'myUserId': this.dataForm.myUserId,
                'myNum': this.dataForm.myNum,
                'myBookName': this.dataForm.myBookName,
                'myBookCover': this.dataForm.myBookCover,
                'myBookContentId': this.dataForm.myBookContentId
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
